package com.welsmann.app.start.spider.process;

import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.welsmann.app.start.spider.entity.SimpleAirQuality;
import com.welsmann.app.start.spider.service.WeatherService;

public class WeatherSpider extends BaseSpider {

	private final static String _aqi_url = "http://222.190.111.117:8023/";
	
	private static final Log log = LogFactory.getLog(WeatherSpider.class);

	@Override
	public void run() {
		try {
			Document aqi = Jsoup.connect(_aqi_url).userAgent(agent).timeout(30000).get();
			Elements aqs = aqi.select("div.air");
			if (aqs != null && !aqs.isEmpty()) {
				SimpleAirQuality saq = new SimpleAirQuality();
				Element aq = aqs.first();
				Elements times = aq.select("p.time");
				saq.setPublish(times.first().text());
				Elements status = aqs.select("p.Status");
				saq.setStatus(status.first().text());
				Elements number = aqs.select("div.number");
				saq.setNumber(number.first().text());
				WeatherService.saveSimpleAirQuality(saq);
			} else {
				log.error("无法获取城市AQI指数。");
			}
			
		} catch (IOException e) {
			log.error("获取空气AQI指数时发生了错误，以下是错误堆栈：");
			e.printStackTrace();
		}
	}

}
